﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmCauHinhChuDeBatBuoc : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmCauHinhChuDeBatBuoc()
        {
            InitializeComponent();
            this.Load();
            //comboBox1_SelectionChangeCommitted(null, null);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {
            msql = "Select * from tb_mondaotao order by Mondaotao";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            cmon.DataSource = dt;
            cmon.DisplayMember = "mondaotao";
            cmon.ValueMember = "ms_mondaotao"; 
        }

        private void cchuongtrinhdt_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select a.Ms_ChudeCT,a.ChudeCT,a.ghichu from tb_LoaichudeCT a inner join " +
                   " Tb_chude_batbuoc b on a.Ms_ChudeCT=b.Ms_ChudeCT inner join tb_cauhinh_chude c" +
                   " on c.MS_mondaotao=b.MS_mondaotao and c.ms_loaichude=a.ms_Loaichude " +
                   " where B.ms_Mondaotao="+cmon.SelectedValue.ToString()+" Order by a.ChudeCT";
            DataTable dt1 = mLibrary.GetDataTable(msql, "table");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;


            msql = "Select a.Ms_ChudeCT,a.ChudeCT,a.ghichu from tb_LoaichudeCT a where Ms_ChudeCT in " +
                   " ( Select a.Ms_ChudeCT from tb_LoaichudeCT a inner join tb_Cauhinh_Chude b " +
                   " on a.MS_Loaichude=b.MS_LoaiChude where B.ms_Mondaotao=" + cmon.SelectedValue.ToString() + " ) and Ms_ChudeCT not in " +
                   " ( Select a.Ms_ChudeCT from tb_LoaichudeCT a inner join Tb_chude_batbuoc b " +
                   " on a.MS_CHudeCT=b.MS_CHudeCT where B.ms_Mondaotao="+cmon.SelectedValue.ToString()+" ) Order by a.ChudeCT ";
            DataTable dt2 = mLibrary.GetDataTable(msql, "table");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
        }

  

        private void butup_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView2.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView2.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    DataGridViewRow row = dataGridView2.CurrentRow;
                    dataGridView2.Rows.Remove(row);
                    //DataTable dt2 = (DataTable)dataGridView2.DataSource;
                    //dt2.Rows[dataGridView2.CurrentRow.Index].Delete();


                    //dataGridView2.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butdown_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView2.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    //DataTable dt2 = (DataTable)dataGridView1.DataSource;
                    //dt2.Rows[dataGridView1.CurrentRow.Index].Delete();
                    //dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {                  
                    //Xóa thông tin củ
                    msql = "Delete Tb_Chude_Batbuoc Where ms_Mondaotao="+cmon.SelectedValue.ToString();
                    mLibrary.RunProc(msql);
                    //cập nhật thông tin mới
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count ; i++)
                        {
                            string MS_Loaichude = "";
                            try
                            {
                                MS_Loaichude = dt.Rows[i]["Ms_chudect"].ToString();
                                msql = "Insert into Tb_Chude_Batbuoc Select 17 as MS_Mondaotao, MS_ChudeCT, getdate()" +
                                        " as Timestamp from tb_LoaichudeCT where MS_ChudeCT in (" + MS_Loaichude + " ) and MS_ChudeCT " +
                                        " not in  (Select MS_ChudeCT from Tb_Chude_Batbuoc Where MS_mondaotao="+cmon.SelectedValue.ToString()+" )";
                                mLibrary.RunProc(msql);   
                            }
                            catch (Exception)
                            {
                                string a = "";
                            }
                                                   
                        }
                    }                    
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butLuu_Click_1(object sender, EventArgs e)
        {
            butLuu_Click(null, null);
        }          
     
    }
}
